package com.enation.app.javashop.consumer.shop.trade.consumer;

import com.enation.app.javashop.consumer.core.event.OrderStatusChangeEvent;
import com.enation.app.javashop.core.base.message.OrderStatusChangeMsg;
import com.enation.app.javashop.core.promotion.groupbuy.service.GroupbuyGoodsManager;
import com.enation.app.javashop.core.promotion.tool.model.dto.PromotionDTO;
import com.enation.app.javashop.core.promotion.tool.model.enums.PromotionTypeEnum;
import com.enation.app.javashop.core.trade.cart.model.vo.CartPromotionVo;
import com.enation.app.javashop.core.trade.order.model.enums.OrderOutStatusEnum;
import com.enation.app.javashop.core.trade.order.model.enums.OrderOutTypeEnum;
import com.enation.app.javashop.core.trade.order.model.enums.OrderStatusEnum;
import com.enation.app.javashop.core.trade.order.model.dos.OrderDO;
import com.enation.app.javashop.core.trade.order.model.vo.OrderSkuVO;
import com.enation.app.javashop.core.trade.order.service.OrderOutStatusManager;
import com.enation.app.javashop.framework.util.JsonUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 团购商品库存变更
 *
 * @author Snow create in 2018/5/22
 * @version v2.0
 * @since v7.0.0
 */
@Component
public class OrderChangeGroupBuyConsumer implements OrderStatusChangeEvent {

    protected final Log logger = LogFactory.getLog(this.getClass());


    @Autowired
    private GroupbuyGoodsManager groupbuyGoodsManager;

    @Autowired
    private OrderOutStatusManager orderOutStatusManager;

    @Override
    public void orderChange(OrderStatusChangeMsg orderMessage) {

        //订单确认，优惠库存扣减
        if (orderMessage.getNewStatus().name().equals(OrderStatusEnum.CONFIRM.name())) {

            OrderDO orderDO = orderMessage.getOrderDO();
            List<OrderSkuVO> orderSkuVOList = JsonUtil.jsonToList(orderDO.getItemsJson(), OrderSkuVO.class);

            List<PromotionDTO> promotionDTOList = new ArrayList<>();
            //如果扣减出错，则回滚库存
            try {
                for (OrderSkuVO orderSkuVO : orderSkuVOList) {
                    List<CartPromotionVo> singleList = orderSkuVO.getSingleList();
                    if (singleList != null && singleList.size() > 0) {
                        for (CartPromotionVo promotionGoodsVO : singleList) {
                            if (promotionGoodsVO.getIsCheck() == 1 && promotionGoodsVO.getPromotionType().equals(PromotionTypeEnum.GROUPBUY.name())) {
                                PromotionDTO promotionDTO = new PromotionDTO();
                                promotionDTO.setActId(promotionGoodsVO.getActivityId());
                                promotionDTO.setGoodsId(orderSkuVO.getGoodsId());
                                promotionDTO.setNum(orderSkuVO.getPurchaseNum());
                                promotionDTOList.add(promotionDTO);
                            }
                        }
                    }
                }
                this.groupbuyGoodsManager.cutQuantity(orderDO.getSn(), promotionDTOList);
                this.orderOutStatusManager.edit(orderDO.getSn(), OrderOutTypeEnum.GROUPBUY_GOODS, OrderOutStatusEnum.SUCCESS);
            } catch (Exception e) {
                logger.error("团购库存扣减失败:", e);
                this.orderOutStatusManager.edit(orderDO.getSn(), OrderOutTypeEnum.GROUPBUY_GOODS, OrderOutStatusEnum.FAIL);
            }

        } else if (orderMessage.getNewStatus().name().equals(OrderStatusEnum.CANCELLED.name())) {
            groupbuyGoodsManager.addQuantity(orderMessage.getOrderDO().getSn());
        }

    }

}
